﻿using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace HAP_Demo
{
    class Program
    {
        //网页的地址,网站每一页的地址
        private static List<string> pagesUrl = new List<string> { "https://www.mzitu.com/" };

        //图片集名称和地址的字典
        private static Dictionary<string, string> dicPics = new Dictionary<string, string>();        

        static void Main(string[] args)
        {
            //声明 HtmlDocument 对象
            HtmlDocument doc = GetDoc(pagesUrl[0]);
            //获取网站最大页数
            int maxPage = Convert.ToInt32(GetMaxPages(doc));
            //生成网站每一个的链接
            for (int i = 2; i <= maxPage; i++)
            {
                pagesUrl.Add($"{pagesUrl[0]}/page/{i}/");
            }
            //对每一页的地址获取图片集的地址
            for (int i = 0; i < pagesUrl.Count; i++)
            {
                //获取图片集的地址
                GetPics(pagesUrl[i]);
                Thread.Sleep(2 * 1000);  //延时2秒
            }
            Console.ReadKey();
        }

        /// <summary>
        /// 获取图片集的地址
        /// </summary>
        /// <param name="doc">HtmlDocument 对象</param>
        private static void GetPics(string Url)
        {
            HtmlDocument doc = GetDoc(Url);

            HtmlNodeCollection htmlNodes = doc.DocumentNode.SelectNodes("//*[@id='pins']/li");
            foreach (HtmlNode item in htmlNodes)
            {
                string sHref = item.ChildNodes.FindFirst("a").Attributes["href"].Value;
                string sSrc = item.ChildNodes.FindFirst("img").Attributes["data-original"].Value;
                string sInnerText = item.InnerText;
                Console.WriteLine($"{sInnerText}:\t{sHref}:\t{sSrc}");
                dicPics.Add(sInnerText, sHref);                
            }
        }
        /// <summary>
        /// 获取HtmlDocument 对象
        /// </summary>
        /// <returns>HtmlDocument 对象</returns>
        private static HtmlDocument GetDoc(string URL)
        {
            //声明网页对象
            HtmlWeb web = new HtmlWeb
            {
                UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36 Edg/96.0.1054.29"
            };
            //获取网页 html 代码对象
            HtmlDocument doc = web.Load(URL);
            return doc;
        }

        /// <summary>
        /// 获取网站最大页数
        /// </summary>
        /// <param name="doc">HtmlDocument 对象</param>
        /// <returns>最大页数</returns>
        private static string GetMaxPages(HtmlDocument doc)
        {
            HtmlNode maxPage = doc.DocumentNode.SelectSingleNode("/html/body/div[2]/div[1]/div[3]/div/a[4]");
            return maxPage.InnerText;
        }
    }
}
